<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 2018/11/11
 * Time: 14:26
 */

namespace app\common\controller;

use app\common\util\WechatThirdUtil;
use app\common\util\WechatAuthorUtil;
use think\Db;
use think\Image;
use app\index\controller\Msg as MsgController;

class WechatBase
{

    private $wx_user; //第三方平台
    private $author_wx_user; //授权公众号
    private $media_ids = [];
    private $wechatObj;
    private $request_uri;
    private $appid;
    private $saveSendKwReply = [];
    private $kw_reply_type = false;

    public function __construct($appid)
    {
        $this->appid = $appid;
        $this->request_uri = NODE_URL.'/receiveMsg';
        $this->request_uri3 = NEW_NODE_URL.'/receiveMsg';
        $this->wx_user = M('wx_user')->find();
        $this->author_wx_user = M('author_wx_user')->where('appid', $appid)->where('authorized', 1)->find();
        $this->wechatObj = new WechatThirdUtil($this->wx_user, $this->author_wx_user);
    }

    /**
     * 处理接收推送消息.
     */
    public function handleMessage()
    {
        /*
         * 微信开放平台全网发布下面代码应注释掉 start
         */
        ignore_user_abort(true);
        ob_start(); //打开输出控制缓冲
        echo 'success'; // 返给微信服务器的字符串
        header('Connection: close'); //关闭http
        header('Content-Length: '.ob_get_length());
        header('X-Accel-Buffering: no'); //nginx
        ob_end_flush();
        ob_flush();
        flush();
        /**
         * end.
         */
        $wechatObj = $this->wechatObj;
        $msg = $wechatObj->handleMessage();
        if (!$msg) {
            exit($wechatObj->getError());
        }

        if ($msg['MsgType'] == 'event') {
            if ($msg['Event'] == 'subscribe') {
                $ret = $this->handleSubscribeEvent($msg); //关注事件
                exit();
                // $this->ajaxReturn($ret);
            } elseif ($msg['Event'] == 'unsubscribe') {
                $this->handleUnSubscribeEvent($msg); //取消关注事件
            } elseif ($msg['Event'] == 'SCAN') {
                $this->SendMsgForCustomerService($msg);
                exit();
                //已关注事件
            } elseif ($msg['Event'] == 'CLICK') {
                $this->handleCallService($msg); //点击事件
                exit();
                // $this->handleClickEvent($msg);//点击事件
            } elseif ($msg['Event'] == 'VIEW') {
                $this->SendMsgForCustomerService($msg);
                exit();
                // $this->handleClickEvent($msg);//点击事件
            } elseif ($msg['Event'] == 'LOCATION') {
                $this->handleLocationEvent($msg);
                exit();
            }
        } elseif ($msg['MsgType'] == 'text') {
            $this->handleTextMsg($msg); //用户输入文本
            exit();
        } elseif ($msg['MsgType'] == 'image') {
            $this->handleImgMsg($msg); //接收用户图片消息
            exit();
        } elseif ($msg['MsgType'] == 'voice') {
            $this->handleVoiceMsg($msg); //接收用户语音消息
            exit();
        } elseif ($msg['MsgType'] == 'video') {
            //接收视频消息
            $this->SendMsgForCustomerService($msg);
            exit();
        } elseif ($msg['MsgType'] == 'shortvideo') {
            //接收小视频消息
            $this->SendMsgForCustomerService($msg);
            exit();
        } elseif ($msg['MsgType'] == 'location') {
            $this->handleLocationEvent($msg);
            exit();
        }
        $this->replyDefault($msg);
        // $this->defaultText($msg);
    }

    /**
     * 地理位置消息.
     */
    private function handleLocationEvent($msg)
    {
        $openid = $msg['FromUserName'];
        $data = [
            'latitude' => $msg['Latitude'],
            'longitude' => $msg['Longitude'],
            'precision' => $msg['Precision'],
        ];
        $subscribe_time = M('users')->where('openid', $openid)->update($data);
    }

    /**
     * 处理取消关注事件.
     *
     * @param array $msg
     */
    private function handleUnSubscribeEvent($msg)
    {
        $openid = $msg['FromUserName'];
        $subscribe_time = M('users')->where('openid', $openid)->value('subscribe_time');
        $update_data = [
            'unsubscribe_time' => time(),
            'subscribe' => 0,
            'repeat_subscribe' => 0,
        ];
        if (time() - $subscribe_time < 10) {
            $update_data['is_second_unsubscribe'] = 1;
        }
        //保存用户足迹
        M('footprint')->insert(['openid' => $openid, 'action' => 2, 'action_time' => time()]);
        $result = Db::name('users')->where('openid', $openid)->update($update_data);
        if ($result) {
            //实时监控关注数量
            $this->mo_wxmsg_type('current_subscribe_count');
            //第三方客服平台接口
            $this->SendMsgForCustomerService($msg);
        }
    }

    /**
     * 处理关注事件.
     *
     * @param array $msg
     *
     * @return type
     */
    private function handleSubscribeEvent($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];
        $openid = $msg['FromUserName'];

        //保存用户足迹
        M('footprint')->insert(['openid' => $openid, 'action' => 1, 'action_time' => time()]);

        if (!$openid) {
            return ['status' => -1, 'msg' => 'openid无效'];
        }

        if ($msg['MsgType'] != 'event' || $msg['Event'] != 'subscribe') {
            return ['status' => 1, 'msg' => '不是关注事件'];
        }

        $wechatObj = $this->wechatObj;
        // 判断是否是新用户关注
        if (!($user = Db::name('users')->where('openid', $openid)->find())) {
            // 新用户
            if (false === ($wxdata = $wechatObj->getFanInfo($openid))) {
                return ['status' => -1, 'msg' => $wechatObj->getError()];
            }

            $user = [
                'openid' => $openid,
                'auid' => $this->author_wx_user['id'],
                'headimgurl' => $wxdata['headimgurl'],
                'nickname' => $wxdata['nickname'] ?: '微信用户',
                'sex' => $wxdata['sex'] ?: 0,
                'subscribe' => $wxdata['subscribe'],
                'oauth' => 'weixin',
                'subscribe_time' => time(),
                'token' => md5(time().mt_rand(1, 99999)),
            ];
            isset($wxdata['unionid']) && $user['unionid'] = $wxdata['unionid'];

            if (isset($msg['EventKey'])) {
                $scene_id = str_replace('qrscene_', '', $msg['EventKey']);
                $user['origin_qr'] = trim($scene_id);
            }
            // 插入新用户到数据库
            $ret = Db::name('users')->insert($user);
            if (!$ret) {
                $data_list = ['openid' => $openid,
                    'cs_id' => 4006,
                    'create_time' => time(),
                    'content' => '插入新用户失败'];
                M('logs')->insert($data_list);
            }
        } else {
            // 更改老用户的关注状态
            Db::name('users')->where('openid', $openid)->update(['is_second_unsubscribe' => 0, 'subscribe' => 1, 'repeat_subscribe' => 1, 'subscribe_time' => time()]);
        }

        // 发送被关注回复
        if ($msg['Event'] == 'subscribe') {
            $content = M('subscribereply')->where('auid', $this->author_wx_user['id'])->value('content');
            if ($content) {
                $content = html_entity_decode($content);
                $resultStr = $this->wechatObj->sendMsgToOne($openid, $content);
            }
        }

        //实时监控关注数量
        $this->mo_wxmsg_type('current_subscribe_count');

        //第三方客服平台接口
        $this->SendMsgForCustomerService($msg);

        /*
         * 获取在线客服列表,根据会话较少的客服进行优先接入
         */
        //获取在线客服列表
        // $online = $wechatObj->getOnlineCustList();
        // if($online['kf_online_list'] == []){
        //     $content = M('subscribereply')->where('auid',$this->author_wx_user['id'])->value('content');
        //     if($content){
        //         $content = html_entity_decode($content);
        //         $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        //         exit($resultStr);
        //     }
        // }

        // $accepted_case_arr = [];
        // foreach($online['kf_online_list'] as $k=>$v){
        //     $accepted_case_arr[$k] = $v['accepted_case'];
        // }

        // $min_acc = min($accepted_case_arr);

        // foreach($accepted_case_arr as $ko=>$vo){
        //     if($vo == $min_acc){
        //         $kfAccount = $online['kf_online_list'][$ko]['kf_account'];
        //         $result = $wechatObj->CreateConversation($kfAccount,$openid);

        //         $content = M('subscribereply')->where('auid',$this->author_wx_user['id'])->value('content');
        //         if($content){
        //             $content = html_entity_decode($content);
        //             $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        //             exit($resultStr);
        //         }
        //     }
        // }
    }

    /**
     * 处理点击事件呼唤客服.
     *
     * @param type $msg
     */
    private function handleCallService($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];

        //第三方客服平台接口
        $this->SendMsgForCustomerService($msg);

        // $wechatObj = $this->wechatObj;
        // $eventKey = $msg['EventKey'];
        // $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $eventKey);
        // if($eventKey == 'V1001_CALL_SERVICE'){
        //     /**
        //      * 获取在线客服列表,根据会话较少的客服进行优先接入
        //      */
        //     //获取在线客服列表
        //     $online = $wechatObj->getOnlineCustList();
        //     if($online['kf_online_list'] == []){
        //         $this->replyDefault($msg);
        //     }

        //     $accepted_case_arr = [];
        //     foreach($online['kf_online_list'] as $k=>$v){
        //         $accepted_case_arr[$k] = $v['accepted_case'];
        //     }

        //     $min_acc = min($accepted_case_arr);

        //     foreach($accepted_case_arr as $ko=>$vo){
        //         if($vo == $min_acc){
        //             $kfAccount = $online['kf_online_list'][$ko]['kf_account'];
        //             $openid = $msg['FromUserName'];
        //             $result = $wechatObj->CreateConversation($kfAccount,$openid);

        //             $content = "请问您有什么肌肤问题呢?";
        //             $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        //             exit($resultStr);
        //         }
        //     }
        // }
    }

    /**
     * 处理点击事件.
     *
     * @param type $msg
     */
    private function handleClickEvent($msg)
    {
        $eventKey = $msg['EventKey'];
        $distribut = tpCache('distribut');

        //分销二维码图片
        if ($distribut['qrcode_menu_word'] && $eventKey == $distribut['qrcode_menu_word']) {
            $this->replyMyQrcode($msg);
        }

        //其他处理
        $this->handleTextMsg($msg);
    }

    /**
     * 回复我的二维码
     */
    private function replyMyQrcode($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;

        if (!($user = M('users')->where('openid', $fromUsername)->find())) {
            $content = '请进入商城: '.SITE_URL.' , 再获取二维码哦';
            $reply = $wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
            exit($reply);
        }

        //获取缓存的图片id
        $distribut = tpCache('distribut');
        $mediaId = $this->getCacheQrcodeMedia($user['user_id'], $user['head_pic'], $distribut['qr_big_back']);
        if (!$mediaId) {
            $mediaId = $this->createQrcodeMedia($msg, $user['user_id'], $user['head_pic'], $distribut['qr_big_back']);
        }

        //回复图片消息
        $reply = $wechatObj->createReplyMsgOfImage($toUsername, $fromUsername, $mediaId);
        exit($reply);
    }

    private function createQrcodeMedia($msg, $userId, $headPic, $qrBackImg)
    {
        $wechatObj = $this->wechatObj;

        //创建二维码关注url
        $qrCode = $wechatObj->createTempQrcode(2592000, $userId);
        if (!(is_array($qrCode) && $qrCode['url'])) {
            $this->replyError($msg, '创建二维码失败');
        }

        //创建分销二维码图片
        $shareImg = $this->createShareQrCode('.'.$qrBackImg, $qrCode['url'], $headPic);
        if (!$shareImg) {
            $this->replyError($msg, '生成图片失败');
        }

        //上传二维码图片
        if (!($mediaInfo = $wechatObj->uploadTempMaterial($shareImg, 'image'))) {
            @unlink($shareImg);
            $this->replyError($msg, '上传图片失败');
        }
        @unlink($shareImg);

        $this->setCacheQrcodeMedia($userId, $headPic, $qrBackImg, $mediaInfo);

        return $mediaInfo['media_id'];
    }

    private function getCacheQrcodeMedia($userId, $headPic, $qrBackImg)
    {
        $symbol = md5("{$headPic}:{$qrBackImg}");
        $mediaIdCache = "distributQrCode:{$userId}:{$symbol}";
        $config = cache($mediaIdCache);
        if (!$config) {
            return false;
        }

        //$config = json_decode($config);
        //有效期3天（259200s）,提前5小时(18000s)过期
        if (!(is_array($config) && $config['media_id'] && ($config['created_at'] + 259200 - 18000) > time())) {
            return false;
        }

        return $config['media_id'];
    }

    private function setCacheQrcodeMedia($userId, $headPic, $qrBackImg, $mediaInfo)
    {
        $symbol = md5("{$headPic}:{$qrBackImg}");
        $mediaIdCache = "distributQrCode:{$userId}:{$symbol}";
        cache($mediaIdCache, $mediaInfo);
    }

    /**
     * 处理点击推送事件.
     *
     * @param array $msg
     */
    private function handleTextMsg($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];
        $keyword = trim($msg['Content']);
        $wechatObj = $this->wechatObj;

        //点击菜单拉取消息时的事件推送
        /*
         * 1、click：点击推事件
         * 用户点击click类型按钮后，微信服务器会通过消息接口推送消息类型为event的结构给开发者（参考消息接口指南）
         * 并且带上按钮中开发者填写的key值，开发者可以通过自定义的key值与用户进行交互；
         */
        if ($msg['MsgType'] == 'event' && $msg['Event'] == 'CLICK') {
            $keyword = trim($msg['EventKey']);
        }

        if (empty($keyword)) {
            return false;
        }


        // 海草回复系统
//        $isspirt = M('author_wx_user')->where('appid',$this->appid)->value('isspirit');
//        if($isspirt){
//            // 开启了海草精灵  $keyword//  1 判断当前用户信息是否在msg存在记录  $fromUsername
//            $msg_controller = new MsgController();
//            $result = $msg_controller->dealmsg($fromUsername,$msg['Content'],$this->appid);
//            // 给客服发送海草精灵回复的消息
//            $keyword_result = $this->handleEvent($msg);
//            // fromname 消息来源用户名
//            $type = $keyword_result['type'];
//            $openid = $msg['FromUserName'];
//            $butt_id = M('users')->where('openid', $openid)->value('butt_id');
//            $post_arr = [
//                'type' => $type,
//                'content' => $result, //消息文本
//                'openid' => $openid, //用户openid
//                'appid' => $this->author_wx_user['appid'], //对应公众号appid
//                'send_time' => time(), //发送时间
//                'send_type' => '1002',
//                'cs_id' => $butt_id,
//                'fromname' => '海草精灵',  // 消息来自于海草精灵
//                'needsend' => 1
//            ];
//            // 判断客服是否是组长, 如果是就不再次发布, 否则需要给组长发消息
//            $line_result = M('csuser')->field('id,pid,type,auto_reply_text,auto_reply_img,auto_reply_img_media_id,poster,poster_media_id')->where('id', $butt_id)->find();
//            if ($line_result['pid'] == 0) {
//                $post_arr['pid'] = $butt_id;
//            } else {
//                $post_arr['pid'] = $line_result['pid'];
//            }
//            $sqlname = 'chatlog'.$butt_id % 10;
//
//            $this->wechatObj->sendMsgToOne($fromUsername,html_entity_decode($result));
////            $send_old = $this->send($this->request_uri,$post_arr);
//            $send_new = $this->send($this->request_uri3,$post_arr);
//            // 把用户消息写入数据库
//            $ret = Db::connect('db_config_chatlog')->name($sqlname)->insert($post_arr);
//        }else{
//        }

        //获取数据库关键词
        $kw_reply = M('kw_reply')->where('auid', $this->author_wx_user['id'])->select();
        if ($kw_reply) {
            foreach ($kw_reply as $reply) {
                $keyword_list_info = unserialize($reply['keyword_list_info']);
                $reply_list_info = unserialize($reply['reply_list_info']);
                foreach ($keyword_list_info as $keyword_info) {
                    if ($keyword_info['match_mode'] == 'equal') {//全匹配
                        if ($keyword_info['content'] == $keyword) {
                            $this->kw_reply($reply, $reply_list_info, $fromUsername);
                            $this->kw_reply_type = true;
                            break 2;
                        }
                    } elseif ($keyword_info['match_mode'] == 'contain') {//半匹配
                        $is_exist = strpos($keyword, $keyword_info['content']);
                        if ($is_exist !== false) {
                            $this->kw_reply($reply, $reply_list_info, $fromUsername);
                            $this->kw_reply_type = true;
                            break 2;
                        }
                    }
                }
            }
        }

        /*
         * 全网发布接入检测普通文本回复
         */
        if ($keyword == 'TESTCOMPONENT_MSG_TYPE_TEXT') {
            $content = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback';
            $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
            exit($resultStr);
        }

        /*
         * 全网发布接入检测api文本回复
         */
        if (strstr($keyword, 'QUERY_AUTH_CODE:')) {
            $query_auth_code = trim(str_replace('QUERY_AUTH_CODE:', '', $keyword));
            file_put_contents('./requestMsg.log', 'query_auth_code:'.$query_auth_code."\n", FILE_APPEND);
            $author_wx_user = new WechatAuthorUtil($this->wx_user);
            $authorizer = $author_wx_user->query_auth($query_auth_code);
            file_put_contents('./requestMsg.log', 'authorizer_access_token:'.$authorizer['authorization_info']['authorizer_access_token']."\n", FILE_APPEND);
            $authorizer_access_token = $authorizer['authorization_info']['authorizer_access_token'];
            $content = $query_auth_code.'_from_api';
            file_put_contents('./requestMsg.log', 'content:'.$content."\n", FILE_APPEND);
            $this->wechatObj->sendMsgToOneCheck($authorizer_access_token, $fromUsername, $content);

            return true;
        }

        //第三方客服平台接口
        $this->SendMsgForCustomerService($msg);
    }

    //关键词回复
    private function kw_reply($reply, $reply_list_info, $openid)
    {
        if ($reply['mode'] == 'random_one') {//随机发送一条
            $rand = array_rand($reply_list_info, 1);
            if ($reply_list_info[$rand]['type'] == 'text') {//发送文本消息
                $this->wechatObj->sendMsgToOne($openid, html_entity_decode($reply_list_info[$rand]['content']));
            } elseif ($reply_list_info[$rand]['type'] == 'mpnews' || $reply_list_info[$rand]['type'] == 'image') {//发送图片消息
                $this->wechatObj->sendMediaToOne($openid, $reply_list_info[$rand]['content'], 'mpnews');
            }//继续判断资源文件类型
            $this->saveSendKwReply($reply_list_info[$rand], $openid);
        } elseif ($reply['mode'] == 'reply_all') {//全部发送
            foreach ($reply_list_info as $r) {
                if ($r['type'] == 'text') {
                    $this->wechatObj->sendMsgToOne($openid, html_entity_decode($r['content']));
                } elseif ($r['type'] == 'mpnews' || $r['type'] == 'image') {//发送图片消息
                    $this->wechatObj->sendMediaToOne($openid, $r['content'], $r['type']);
                }//继续判断资源文件类型
                $this->saveSendKwReply($r, $openid);
            }
        }
    }

    private function saveSendKwReply($reply, $openid)
    {
        $arr = [
            'type' => $reply['type'],
            'openid' => $openid, //用户openid
            'appid' => $this->author_wx_user['appid'], //对应公众号appid
            // 'send_time' => time(), //发送时间
            'send_type' => '1002',
        ];

        if ($reply['type'] == 'mpnews') {
            $arr['content'] = $reply['desc']['news_item']; //消息图文
        } elseif ($reply['type'] == 'text') {
            $arr['content'] = html_entity_decode($reply['content']); //消息文本
        } elseif ($reply['type'] == 'image') {
            $arr['content'] = $reply['url'];
        }
        $this->saveSendKwReply[] = $arr;
    }

    private function setKwReplyButtId($butt_id, $needsend, $pid)
    {
        foreach ($this->saveSendKwReply as $key => $kw) {
            $this->saveSendKwReply[$key]['pid'] = $pid;
            $this->saveSendKwReply[$key]['cs_id'] = $butt_id;
            $this->saveSendKwReply[$key]['send_time'] = time();
            $this->saveSendKwReply[$key]['needsend'] = $needsend;
        }
    }

    /**
     * 处理图片消息.
     */
    private function handleImgMsg($msg)
    {
        //第三方客服平台接口
        $this->SendMsgForCustomerService($msg);
    }

    /**
     * 处理语音消息.
     */
    private function handleVoiceMsg($msg)
    {
        //第三方客服平台接口
        $this->SendMsgForCustomerService($msg);
    }

    /**
     * 新增永久素材.
     *
     * @param array $articles
     *                        [
     *                        [
     *                        "title"=> TITLE,
     *                        "thumb_media_id"=> THUMB_MEDIA_ID,
     *                        "author"=> AUTHOR,
     *                        "digest"=> DIGEST,
     *                        "show_cover_pic"=> SHOW_COVER_PIC(0 / 1),
     *                        "content"=> CONTENT,
     *                        "content_source_url"=> CONTENT_SOURCE_URL
     *                        ],
     *                        //若新增的是多图文素材，则此处应还有几段articles结构(最多8段)
     *                        ]
     *
     * @return MEDIA_ID
     */
    private function addNews()
    {
        // $fromUsername = $msg['FromUserName'];
        // $toUsername   = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;
        $title = '测试';
        $thumb_media_id = 'b8Wx5RH5nQP3NCxocNRG1PTtuSn9V_innMmpd30NvsY';
        $author = '作者是我';
        $digest = '图文消息的摘要';
        $content = "<a href='http://sex.yougou520.cn/mobile'>商城链接</a>";
        $content_source_url = 'http://shop.yougou520.cn/mobile';
        $articles = [
            [
                'title' => $title,
                'thumb_media_id' => $thumb_media_id,
                'author' => $author,
                'digest' => $digest,
                'show_cover_pic' => 1,
                'content' => $content,
                'content_source_url' => $content_source_url,
            ],
        ];
        $result = $wechatObj->uploadNews($articles);
        // $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $result);
        // exit($resultStr);
        return $result;
    }

    /**
     * 默认回复图文消息.
     *
     * @param type $msg
     */
    private function defaultText($msg)
    {
        $openid = $msg['FromUserName'];
        $wechatObj = $this->wechatObj;
        $media_id = 'zGHyeU0F7LTXvE31hETbkV7kqdYQeQdF8Tvw3x9as_E';
        $wechatObj->sendImgMsgToOne($openid, $media_id);
        exit;
    }

    /**
     * 默认回复.
     *
     * @param type $msg
     */
    private function replyDefault($msg)
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;

        $send_time = M('users')->where('openid', $msg['FromUserName'])->value('send_qrcode_time');
        if ($send_time < time() && !empty($this->media_ids)) {
            M('users')->where('openid', $msg['FromUserName'])->update(['send_qrcode_time' => time() + 7200]);
            $wechatKey = array_rand($this->media_ids, 1);
            $wechatObj->sendMediaToOne($msg['FromUserName'], $this->media_ids[$wechatKey], 'image');
        }

        $content = M('defaultreply')->where('auid', $this->author_wx_user['id'])->value('content');
        if ($content) {
            $content = html_entity_decode($content);
            $resultStr = $this->wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
            exit($resultStr);
        }
    }

    private function replyError($msg, $extraMsg = '')
    {
        $fromUsername = $msg['FromUserName'];
        $toUsername = $msg['ToUserName'];
        $wechatObj = $this->wechatObj;

        if ($wechatObj->isDedug()) {
            $content = '错误信息：';
            $content .= $wechatObj->getError() ?: '';
            $content .= $extraMsg ?: '';
        } elseif ($extraMsg) {
            $content = '系统信息：'.$extraMsg;
        } else {
            $content = '系统正在处理...';
        }

        $resultStr = $wechatObj->createReplyMsgOfText($toUsername, $fromUsername, $content);
        exit($resultStr);
    }

    /**
     * 创建分享二维码图片.
     *
     * @param type $backImg 背景大图片
     * @param type $qrText  二维码文本:关注入口
     * @param type $headPic 头像路径
     *
     * @return 图片路径
     */
    private function createShareQrCode($backImg, $qrText, $headPic)
    {
        if (!is_file($backImg) || !$headPic || !$qrText) {
            return false;
        }

        vendor('phpqrcode.phpqrcode');
        vendor('topthink.think-image.src.Image');

        $qr_code_path = './public/upload/qr_code/';
        !file_exists($qr_code_path) && mkdir($qr_code_path, 0777, true);

        /* 生成二维码 */
        $qr_code_file = $qr_code_path.time().rand(1, 10000).'.png';
        \QRcode::png($qrText, $qr_code_file, QR_ECLEVEL_M);

        $QR = Image::open($qr_code_file);
        $QR_width = $QR->width();
        $QR_height = $QR->height();

        /* 添加背景图 */
        if ($backImg && is_file($backImg)) {
            $back = Image::open($backImg);
            $backWidth = $back->width();
            $backHeight = $back->height();

            //生成的图片大小以540*960为准
            if ($backWidth <= $backHeight) {
                $refWidth = 540;
                $refHeight = 960;
                if (($backWidth / $backHeight) > ($refWidth / $refHeight)) {
                    $backRatio = $refWidth / $backWidth;
                    $backWidth = $refWidth;
                    $backHeight = $backHeight * $backRatio;
                } else {
                    $backRatio = $refHeight / $backHeight;
                    $backHeight = $refHeight;
                    $backWidth = $backWidth * $backRatio;
                }
            } else {
                $refWidth = 960;
                $refHeight = 540;
                if (($backWidth / $backHeight) > ($refWidth / $refHeight)) {
                    $backRatio = $refHeight / $backHeight;
                    $backHeight = $refHeight;
                    $backWidth = $backWidth * $backRatio;
                } else {
                    $backRatio = $refWidth / $backWidth;
                    $backWidth = $refWidth;
                    $backHeight = $backHeight * $backRatio;
                }
            }

            $shortSize = $backWidth > $backHeight ? $backHeight : $backWidth;
            $QR_width = $shortSize / 2;
            $QR_height = $QR_width;
            $QR->thumb($QR_width, $QR_height, \think\Image::THUMB_CENTER)->save($qr_code_file, null, 100);
            $back->thumb($backWidth, $backHeight, \think\Image::THUMB_CENTER)
                ->water($qr_code_file, \think\Image::WATER_CENTER, 90)->save($qr_code_file, null, 100);
            $QR = $back;
        }

        /* 添加头像 */
        if ($headPic) {
            //如果是网络头像
            if (strpos($headPic, 'http') === 0) {
                //下载头像
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $headPic);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $file_content = curl_exec($ch);
                curl_close($ch);
                //保存头像
                if ($file_content) {
                    $head_pic_path = $qr_code_path.time().rand(1, 10000).'.png';
                    file_put_contents($head_pic_path, $file_content);
                    $headPic = $head_pic_path;
                }
            }
            //如果是本地头像
            if (file_exists($headPic)) {
                $logo = Image::open($headPic);
                $logo_width = $logo->height();
                $logo_height = $logo->width();
                $logo_qr_width = $QR_width / 5;
                $scale = $logo_width / $logo_qr_width;
                $logo_qr_height = $logo_height / $scale;
                $logo_file = $qr_code_path.time().rand(1, 10000);
                $logo->thumb($logo_qr_width, $logo_qr_height)->save($logo_file, null, 100);
                $QR = $QR->water($logo_file, \think\Image::WATER_CENTER);
                unlink($logo_file);
            }
            if ($head_pic_path) {
                unlink($head_pic_path);
            }
        }

        //加上有效时间
        $valid_date = date('Y.m.d', strtotime('+30 days'));
        $QR = $QR->text('有效时间 '.$valid_date, './vendor/topthink/think-captcha/assets/zhttfs/1.ttf', 16, '#FFFFFF', Image::WATER_SOUTH)->save($qr_code_file);

        return $qr_code_file;
    }

    /**
     * 向客服发送用户信息.
     *
     * @param $msg
     * @param $this->author_wx_user 公众号信息
     * @param $msg['FromUserName'] 用户opneid
     */
    protected function SendMsgForCustomerService($msg)
    {
        //通过公众号id检测客服对接人数，以对接的平均值分配客服
        $needsend = 0;
        $is_send_poster = false;
        $keyword_result = $this->handleEvent($msg);
        $keyword = $keyword_result['keyword'];
        $type = $keyword_result['type'];

        $openid = $msg['FromUserName'];
        $post_arr = [
            'type' => $type,
            'content' => $keyword, //消息文本
            'openid' => $openid, //用户openid
            'appid' => $this->author_wx_user['appid'], //对应公众号appid
            'send_time' => time(), //发送时间
            'send_type' => '1001',
        ];

        $where = [
            'auid' => ['eq', $this->author_wx_user['id']],
            'status' => ['eq', 1],
        ];

        //如果是二维码关注,判定二维码分配到哪个
        if ($type == 'subscribe') {//用户未关注时，进行关注后的事件推送
            if (isset($msg['EventKey'])) {
                $scene_id = str_replace('qrscene_', '', $msg['EventKey']);
                $where['u_scene_id'] = ['eq', trim($scene_id)];
            }
        } elseif ($type == 'scan') {//用户已关注时的事件推送
            if (isset($msg['EventKey'])) {
                $scene_id = $msg['EventKey'];
                $where['u_scene_id'] = ['eq', trim($scene_id)];
            }
        }

        // 默认分配模式 -----打热线版本---- 分配给固定的客户 情况一 - 有客服在线 优先分配给客服   情况二 - 均无客服在线 平均分配给离线客服
        if ($this->author_wx_user['distribute_type'] == 1) {
            // ---------------------------------------------------------------------- 分配方式一 -----------------------------------------------------------------
            // ------------------------------------------ 优先平均分配给可分配在线客服，如果全部离线，平均分配给可分配离线客服(默认) ----------------------------------
            $butt = M('csuser')->where($where)->where('type', 1)->column('id,today_butt');
        } elseif ($this->author_wx_user['distribute_type'] == 2) {
            // ---------------------------------------------------------------------- 分配方式二 -----------------------------------------------------------------
            // --------------------------------------------------------- 平均分配给所有可分配客服(包括在线和离线) --------------------------------------------------
            $butt = M('csuser')->where($where)->column('id,today_butt');
        }

        if (empty($butt) && $this->author_wx_user['create_cs_count'] == 0) { //如果没有创建客服账号下面也没有客服,就发送默认回复
            $this->replyDefault($msg);
        } elseif (empty($butt) && $this->author_wx_user['create_cs_count'] > 0) {
            //创建了客服账号，但全未上线
            $butt = M('csuser')->where($where)->column('id,today_butt');
            $needsend = 1;
        }

        //先判断用户是否已经有对接客服
        $butt_id = M('users')->where('openid', $openid)->value('butt_id');
        if ($butt_id) {

            // 分配模式
            if ($this->author_wx_user['isspirit'] == 1) {
                // -----追访版本---- 老粉丝主动聊天,但是分配的客服已经不再接广告了,这个时候对老粉丝重新分配给开启分配的新客服
                // 修改客户的 butt_id 为当前分配广告的butt_id ; 随机获取一个开启分配的客服id
                $butt_now = M('csuser')->where(['status'=>1])->limit(1)->value('id');
                $update_butt = M('users')->where('openid', $openid)->update(['butt_id'=>$butt_now]);
                if($update_butt){
                    // 消息发送给新的客服
                    $butt_id = $butt_now;
                    $data_list = ['openid' => $openid,
                        'cs_id' => $butt_id,
                        'create_time' => time(),
                        'content' => '追访模式成功'];
                    M('logs')->insert($data_list);
                }else{
                    //
                    $data_list = ['openid' => $openid,
                        'cs_id' => $butt_id,
                        'create_time' => time(),
                        'content' => '追访模式失败'];
                    M('logs')->insert($data_list);
                }
            }
            //修改最后会话时间，且不是静默用户
            if ($type != 'subscribe' || $type != 'unsubscribe') {
                M('users')->where('openid', $openid)->update(['last_butt_time' => time(), 'is_silent' => 1]);
            }
            $sqlname = 'chatlog'.$butt_id % 10;
            //已有对接客服,把消息传到对应客服
            $post_arr['cs_id'] = $butt_id; //客服id
            $line_result = M('csuser')->field('id,pid,type,auto_reply_text,auto_reply_img,auto_reply_img_media_id,poster,poster_media_id')->where('id', $butt_id)->find();
            if ($line_result) {
                $line_type = $line_result['type'];
                //新用户关注
                if ($type == 'subscribe') {
                    $subReply = $this->posterMsg($openid, $line_result, $butt_id);
                    if (!empty($subReply)) {
                        $is_send_poster = true;
                    }
                }
                if ($type !== 'unsubscribe' && $line_type == 3 && $is_send_poster == false && $this->kw_reply_type == false) {
                    //已有对接客服，但是离线状态下，需要推送
                    $needsend = 1;
                    $outline_result = $this->outlineMsg($openid, $line_result, $butt_id);
                    if ($outline_result === false) {
                        return false;
                    }
                } elseif ($line_type == 3 && $is_send_poster == true) {
                    $needsend = 1;
                    $subReply['needsend'] = $needsend;
                } elseif ($line_type == 1) {
                    //客服可能不在接线，但还上线的状态，所以老用户聊天也需要主动推送用户消息
                    $needsend = 0;
                }
                $post_arr['needsend'] = $needsend;
                // 判断是否是组长账号
                if ($line_result['pid'] == 0) {   // 是组长账号下直接对接的客户,不需要转发
                    $post_arr['pid'] = $butt_id;
                    $this->send($this->request_uri3, $post_arr);
                    // 把用户消息写入数据库,存储到组长发送消息的数据库
                    $ret = Db::connect('db_config_chatlog')->name($sqlname)->insert($post_arr);
                } else { // 是组员对接的客户,需要转发消息给组长
//                    $post_arr['pid'] = $butt_id;
//                    $this->send($this->request_uri3, $post_arr);
                    $post_arr['pid'] = $line_result['pid'];
                    $this->send($this->request_uri3, $post_arr);
                    // 把用户消息写入数据库,存储到组员发送消息的数据库
                    $ret = Db::connect('db_config_chatlog')->name($sqlname)->insert($post_arr);
                }
//                $result = $this->send($this->request_uri, $post_arr);
                // 判断是否匹配关键词回复
                if (!empty($this->saveSendKwReply)) {
                    $this->setKwReplyButtId($butt_id, $needsend, $post_arr['pid']);
                    // 依次遍历需要发送的关键词回复消息
                    foreach ($this->saveSendKwReply as $reply_key => $send_kw_reply) {
                        if (is_array($send_kw_reply['content'])) {
                            $this->saveSendKwReply[$reply_key]['content'] = json_encode($send_kw_reply['content'], JSON_UNESCAPED_UNICODE);
                        }
                        if ($needsend == 0) {
//                            $result = $this->send($this->request_uri, $this->saveSendKwReply[$reply_key]);
                            $this->send($this->request_uri3, $this->saveSendKwReply[$reply_key]);
                        }
                    }
                    Db::connect('db_config_chatlog')->name($sqlname)->insertAll($this->saveSendKwReply);
                }
                // 判断是否需要执行离线消息
                if (!empty($outline_result)) {
                    foreach ($outline_result as $outline_message) {
//                        $this->send($this->request_uri, $outline_message);
                        $this->send($this->request_uri3, $outline_message);
                    }
                    Db::connect('db_config_chatlog')->name($sqlname)->insertAll($outline_result);
                }
                if ($is_send_poster == true) {
                    Db::connect('db_config_chatlog')->name($sqlname)->insert($subReply);
                }
                return true;
            }
        }

        //---------------------------------------------------------------新用户对话----------------------------------------------------------
        $butt_id = $this->getMinButt($butt);
        $update_butt_id = M('users')->where('openid', $openid)->update(['butt_id' => $butt_id, 'create_butt_time' => time(), 'last_butt_time' => time()]); //修改客户对接客服id
        $online_butt = M('csuser')->execute("UPDATE `think_csuser`  SET `today_butt`= $butt[$butt_id] + 1,`online_butt`= online_butt + 1 WHERE  `id` = $butt_id"); //修改客服在线对接数
        if ($update_butt_id && $online_butt) {
            $pid = M('csuser')->where('id', $butt_id)->value('pid');
            if ($pid == 0) {
                $post_arr['pid'] = $butt_id;
            } else {
                $post_arr['pid'] = $pid;
            }
            $post_arr['cs_id'] = $butt_id; //客服id
            $sqlname = 'chatlog'.$butt_id % 10;
            $line_result = M('csuser')->field('id,pid,type,auto_reply_text,auto_reply_img,auto_reply_img_media_id,poster,poster_media_id')->where('id', $butt_id)->find();
            $line_type = $line_result['type'];

            //新用户关注
            if ($type == 'subscribe') {
                $subReply = $this->posterMsg($openid, $line_result, $butt_id);
                if (!empty($subReply)) {
                    $is_send_poster = true;
                }
            }

            if ($line_type == 3 && $is_send_poster == false && $this->kw_reply_type == false) {
                $needsend = 1;
                $outline_result = $this->outlineMsg($openid, $line_result, $butt_id);
                if ($outline_result === false) {
                    return false;
                }
            } elseif ($line_type == 3 && $is_send_poster == true) {
                $needsend = 1;
                $subReply['needsend'] = $needsend;
            }

            $post_arr['needsend'] = $needsend;

//            $this->send($this->request_uri, $post_arr);
            $this->send($this->request_uri3, $post_arr);
            $this->mo_wxmsg_type('current_butt_count'); //实时监听对接人数

            $ret = Db::connect('db_config_chatlog')->name($sqlname)->insert($post_arr);
            if (!empty($outline_result)) {
                foreach ($outline_result as $outline_message) {
//                    $this->send($this->request_uri, $outline_message);
                    $this->send($this->request_uri3, $outline_message);
                }
                Db::connect('db_config_chatlog')->name($sqlname)->insertAll($outline_result);
            }
            if ($is_send_poster == true) {
                Db::connect('db_config_chatlog')->name($sqlname)->insert($subReply);
            }

            return true;
        } else {
            return json(['status' => -1, 'msg' => 'system error']);
        }
    }

    /**
     * 获取对接数最少的客服.
     */
    private function getMinButt($butt)
    {
        $min_acc = min($butt);
        foreach ($butt as $ko => $vo) {
            if ($vo == $min_acc) {
                $butt_id = $ko; //获取对接最少客服id
                break;
            }
        }

        return $butt_id;
    }

    /**
     * 关注回复推广海报.
     */
    private function posterMsg($openid, $poster, $butt_id)
    {
        $subscribeReply = [];
        if (!empty($poster['poster_media_id'])) {
            $sendMsg = $this->wechatObj->sendMediaToOne($openid, $poster['poster_media_id'], 'image');
            if ($sendMsg) {
                $subscribeReply = [
                    'type' => 'image',
                    'content' => $poster['poster'], //图片路径
                    'openid' => $openid, //用户openid
                    'appid' => $this->author_wx_user['appid'], //对应公众号appid
                    'cs_id' => $butt_id, //客服id
                    'send_time' => time(), //发送时间
                    'send_type' => '1002',
                    'needsend' => 0,
                ];
//                $this->send($this->request_uri, $subscribeReply);
                $this->send($this->request_uri3, $subscribeReply);
            } else {
                return false;
            }
        }

        return $subscribeReply;
    }

    /**
     * 给用户发送离线消息.
     */
    public function outlineMsg($openid, $line_result, $butt_id)
    {
        // file_put_contents('./requestMsg.log', '系统Wechat主动发送离线消息,发送客服id:'.$butt_id.'发送给客户openid'.$openid.'发送时间戳:'.time()."\n", FILE_APPEND);
        $outlineReply = [];
        if (!empty($line_result['auto_reply_text'])) {
            $content = html_entity_decode($line_result['auto_reply_text']);
            $sendMsg = $this->wechatObj->sendMsgToOne($openid, $content);
            if ($sendMsg) {
                $outlineReply[] = [
                    'type' => 'text',
                    'content' => $content, //消息文本
                    'openid' => $openid, //用户openid
                    'appid' => $this->author_wx_user['appid'], //对应公众号appid
                    'cs_id' => $butt_id, //客服id
                    'send_time' => time()+1, //发送时间
                    'send_type' => '1002',
                    'needsend' => 1,
                    'pid' => $line_result['pid'] == 0 ? $line_result['id'] : $line_result['pid'],
                ];
            } else {
                return false;
            }
        }

        //且发送客服设置的离线消息
        if (!empty($line_result['auto_reply_img'])) {
            $sendImg = $this->wechatObj->sendMediaToOne($openid, $line_result['auto_reply_img_media_id'], 'image');
            if ($sendImg) {
                $outlineReply[] = [
                    'type' => 'image',
                    'content' => $line_result['auto_reply_img'], //图片路径
                    'openid' => $openid, //用户openid
                    'appid' => $this->author_wx_user['appid'], //对应公众号appid
                    'cs_id' => $butt_id, //客服id
                    'send_time' => time()+2, //发送时间
                    'send_type' => '1002',
                    'needsend' => 1,
                    'pid' => $line_result['pid'] == 0 ? $line_result['id'] : $line_result['pid'],
                ];
            } else {
                return false;
            }
        }

        return $outlineReply;
    }

    /**
     * 发送消息.
     */
    private function send($url, $post_arr)
    {
        $result = httpRequest($url, 'POST', $post_arr);
        if (!$result) {
            return false;
        }

        return $result;
    }

    /**
     * 处理微信事件.
    private function handleEvent($msg)
    {
        $keyword = '';
        $type = '';
        $result = [];
        if ($msg['MsgType'] == 'event') {
            if ($msg['Event'] == 'subscribe') {
                $keyword = '关注';
                $type = 'subscribe';
            } elseif ($msg['Event'] == 'unsubscribe') {
                $keyword = '取消关注';
                $type = 'unsubscribe';
            } elseif ($msg['Event'] == 'SCAN') {
                $keyword = '用户重复扫码关注';
                $type = 'scan';
            } elseif ($msg['Event'] == 'CLICK') {
                if ($msg['EventKey'] == 'V1001_CALL_SERVICE') {
                    if (!empty($this->author_wx_user['welcoming'])) {
                        $this->wechatObj->sendMsgToOne($msg['FromUserName'], $this->author_wx_user['welcoming']);
                    }
                    $keyword = '用户正在呼叫客服...请回复!';
                } else {
                    $keyword = '用户触发点击事件:'.trim($msg['EventKey']);
                }
                $type = 'text';
                $menuData = ['auid' => $this->author_wx_user['id'], 'event_type' => $msg['Event'], 'openid' => $msg['FromUserName'], 'event_key' => trim($msg['EventKey']), 'click_time' => time()];
                Db::name('wechat_menu_click')->insert($menuData);
            } elseif ($msg['Event'] == 'VIEW') {
                //匹配菜单栏名字 获取菜单栏
                $menu = $this->wechatObj->getNowMenu();
                foreach ($menu['menu']['button'] as $btn) {
                    if (isset($btn['url']) && $btn['url'] == trim($msg['EventKey'])) {
                        $name = $btn['name'];
                    } elseif (isset($btn['sub_button'])) {
                        foreach ($btn['sub_button'] as $sub_btn) {
                            if (isset($sub_btn['url']) && $sub_btn['url'] == trim($msg['EventKey'])) {
                                $name = $btn['name'];
                            }
                        }
                    } else {
                        $name = '';
                    }
                }
                $keyword = '用户点击菜单栏按钮:'.$name;
                $type = 'text';
                $menuData = ['auid' => $this->author_wx_user['id'], 'event_type' => $msg['Event'], 'openid' => $msg['FromUserName'], 'event_key' => trim($msg['EventKey']), 'btn_name' => $name, 'click_time' => time()];
                Db::name('wechat_menu_click')->insert($menuData);
            }
        } elseif ($msg['MsgType'] == 'text') {
            $keyword = trim($msg['Content']);
            $type = 'text';
        } elseif ($msg['MsgType'] == 'image') {
            $keyword = trim($msg['PicUrl']);
            $type = 'image';
            // $chat_upload = new Chatupload;
            // $keyword_arr = $chat_upload->saveChatImage($imgUrl);
            // $keyword = $keyword_arr['path'];
            // file_put_contents('./requestMsg.log','PicUrl:'.$keyword."\n",FILE_APPEND);
        } elseif ($msg['MsgType'] == 'voice') {
            $media_id = trim($msg['MediaId']);
            $media_result = $this->saveWeiXinMedia($media_id);
            if ($media_result) {
                $keyword = $media_result;
            } else {
                return false;
            }
            $type = 'voice';
        } elseif ($msg['MsgType'] == 'video' || $msg['MsgType'] == 'shortvideo') {
            //接收视频消息
            $media_id = trim($msg['MediaId']); //视频资源
            $thumb_media_id = trim($msg['ThumbMediaId']); //缩略图资源
            $video = $this->saveWeiXinMedia($media_id);
            $thumb = $this->saveWeiXinMedia($thumb_media_id);
            $keyword = json_encode(['video' => $video, 'thumb' => $thumb], JSON_UNESCAPED_UNICODE);
            $type = 'video';
        }

        $result = ['keyword' => $keyword, 'type' => $type];

        return $result;
    }

    /**
     * amr转MP3格式.
     */
    private function amrEncodeMp3($from, $to, $name)
    {
        $str = '/usr/local/bin/ffmpeg -i '.$from.' '.$to.$name;
        system($str);
    }

    /**
     * 保存视频资源.
     */
    private function saveWeiXinMedia($media_id)
    {
        $check_Encode = false;
        $file = $this->wechatObj->getTemporaryMaterial($media_id);
        $upload_path = str_replace('\\', '/', ROOT_PATH.'public/uploads');
        $filename = $this->savePath($upload_path);
        // 检测目录
        if (false === $this->checkPath($filename)) {
            return false;
        }
        $save_path = '/uploads/';

        if (strpos($file['header']['content_type'], 'video') !== false) {
            $name = md5(microtime(true)).'.mp4';
        } elseif (strpos($file['header']['content_type'], 'image') !== false) {
            $name = md5(microtime(true)).'.jpg';
        } elseif (strpos($file['header']['content_type'], 'audio') !== false) {
            $name = md5(microtime(true)).'.amr';
            $toname = md5(microtime(true)).'.mp3';
            // file_put_contents('./requestMsg.log','toname:'.$toname."\n",FILE_APPEND);
            $check_Encode = true;
        }

        if ($file) {
            $filename_path = $filename.$name;
            $save_result = $this->saveweixinFile($filename_path, $file['body']);
            if ($save_result) {
                if ($check_Encode) {
                    $this->amrEncodeMp3($filename_path, $filename, $toname);

                    return str_replace('\\', '/', $save_path.date('Ymd').DS.$toname);
                } else {
                    return str_replace('\\', '/', $save_path.date('Ymd').DS.$name);
                }
            } else {
                return false;
            }
        }
    }

    /**
     * 文件保存至本地.
     */
    private function saveweixinFile($filename, $filecontent)
    {
        $local_file = fopen($filename, 'w');
        if (false !== $local_file) {
            if (false !== fwrite($local_file, $filecontent)) {
                fclose($local_file);

                return true;
            }
        }

        return false;
    }

    private function savePath($path)
    {
        $rule = 'date';
        $path = rtrim($path, DS).DS;
        switch ($rule) {
            case 'date':
                $saveName = date('Ymd').DS;
                break;
        }
        $filename = $path.$saveName;

        return $filename;
    }

    private function checkPath($path)
    {
        if (is_dir($path)) {
            return true;
        }

        if (mkdir($path, 0755, true)) {
            return true;
        } else {
            $this->error = "目录 {$path} 创建失败！";

            return false;
        }
    }

    //推送消息
    private function mo_wxmsg_type($type)
    {
        $arr_data = [
            'type' => $type,
            'data' => [
                'id' => $this->author_wx_user['id'],
                'uid' => $this->author_wx_user['uid'],
            ],
        ];
        sendWebSocket($arr_data);
    }
}
